package types

import (
	"database/sql"
	"encoding/json"
	"gorm.io/datatypes"
	"time"
)

type ReqMgGetThing struct {
	StationId string `json:"station_id" form:"station_id" binding:"required"`
	BasePage
}

type RespMgGetThing struct {
	BusinessId      string       `json:"business_id"`
	BuyTime         JsonNullTime `json:"buy_time"`
	CatalogId       string       `json:"catalog_id"`
	CreateTime      JsonNullTime `json:"create_time"`
	FirstOnlineTime JsonNullTime `json:"first_online_time"`
	IsProxy         bool         `json:"is_proxy"`
	IsStation       bool         `json:"is_station"`
	Lat             string       `json:"lat"`
	Level           int          `json:"level"`
	Lon             string       `json:"lon"`
	ModelId         string       `json:"model_id"`
	ModelName       string       `json:"model_name"`
	ProtocolPath    string       `json:"protocol_path"`
	ProxyThingId    string       `json:"proxy_thing_id"`
	SName           string       `json:"s_name"`
	Sno             int          `json:"sno"`
	State           int          `json:"state"`       // 1 在线
	StatusUsed      int          `json:"status_used"` // -2 未知;1使用中;0停用;-1施工中;
	Uid             string       `json:"uid"`
	UsedTime        JsonNullTime `json:"used_time"` // 启用时间
}

type JsonNullTime struct {
	sql.NullTime
}

func (v JsonNullTime) MarshalJSON() ([]byte, error) {
	if v.Valid {
		return json.Marshal(v.Time.Format("2006-01-02 15:04:05"))
	} else {
		return json.Marshal("")
	}
}

type Datetime time.Time

func (t Datetime) MarshalJSON() ([]byte, error) {
	return json.Marshal(time.Time(t).Format("2006-01-02 15:04:05"))
}

type RespSelectModels struct {
	ModelId   string         `json:"model_id"`
	ModelName string         `json:"model_name"`
	Params    datatypes.JSON `json:"params"`
	Protocol  []interface{}  `json:"protocol"`
}

type RespSelectEdge struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

type ReqModelProtocol struct {
	ModelId string `json:"model_id" form:"model_id" binding:"required"`
}

type ReqGetThingParam struct {
	ThingId string `json:"thing_id" form:"thing_id" binding:"required"`
	ModelId string `json:"model_id" form:"model_id" binding:"required"`
}

type ReqMgAddThing struct {
	StationId    string                 `json:"station_id" form:"station_id" binding:"required"`       // 站点ID
	ThingId      string                 `json:"thing_id" form:"thing_id"`                              // 设备ID
	SName        string                 `json:"s_name" form:"s_name" binding:"required"`               // 设备昵称
	BusinessId   string                 `json:"business_id" form:"business_id" `                       // 点位编号
	StatusUsed   int                    `json:"status_used" form:"status_used" binding:"gte=-2,lte=2"` // 开通状态 -2 未知;1使用中;0停用;-1施工中; //"gte=0,lte=130"` //  0<=Age<=130
	ProxyThingId string                 `json:"proxy_thing_id" form:"proxy_thing_id"`                  // 接入点  "" 直接连， 代理ID
	Lat          string                 `json:"lat" form:"lat"`                                        // 经度
	Lon          string                 `json:"lon" form:"lon"`                                        // 纬度
	ModelId      string                 `json:"model_id" form:"model_id" binding:"required"`           // 物模型ID
	ProtocolPath string                 `json:"protocol_path" form:"protocol_path"`                    // 协议路径
	ThingParams  map[string]interface{} `json:"thing_params" form:"thing_params"`                      // 设备参数
}

type ReqMgModifyThing struct {
	ThingId      string                 `json:"thing_id" form:"thing_id" binding:"required"`           // 设备ID
	SName        string                 `json:"s_name" form:"s_name" binding:"required"`               // 设备昵称
	BusinessId   string                 `json:"business_id" form:"business_id" `                       // 点位编号
	StatusUsed   int                    `json:"status_used" form:"status_used" binding:"gte=-2,lte=2"` // 开通状态 -2 未知;1使用中;0停用;-1施工中; //"gte=0,lte=130"` //  0<=Age<=130
	ProxyThingId string                 `json:"proxy_thing_id" form:"proxy_thing_id"`                  // 接入点  "" 直接连， 代理ID
	Lat          string                 `json:"lat" form:"lat"`                                        // 经度
	Lon          string                 `json:"lon" form:"lon"`                                        // 纬度
	ModelId      string                 `json:"model_id" form:"model_id" binding:"required"`           // 物模型ID
	ProtocolPath string                 `json:"protocol_path" form:"protocol_path"`                    // 协议路径
	ThingParams  map[string]interface{} `json:"thing_params" form:"thing_params"`                      // 设备参数
}

type ReqDelThing struct {
	ThingId string `json:"thing_id" form:"thing_id" binding:"required"` // 设备ID
}

type ReqGetAreas struct {
	ParentId string `json:"parent_id" form:"parent_id"`
}

type ReqAddStation struct {
	StationId string `json:"station_id" form:"station_id"`
	ParentId  string `json:"parent_id" form:"parent_id"`
	Name      string `json:"s_name" form:"s_name" binding:"required"`
	Lon       string `json:"lon" form:"lon"`
	Lat       string `json:"lat" form:"lat"`
	Desc      string `json:"desc" form:"desc"`
	Level     int    `json:"level" form:"level"`
	AreaId    string `json:"area_id" form:"area_id" binding:"required"`
	ModelId   string `json:"model_id" form:"model_id" binding:"required"`
}

type ReqMgGetStation struct {
	StationId string `json:"station_id" form:"station_id" binding:"required"`
}

type RespMgGetStation struct {
	StationId  string `json:"station_id" form:"station_id"`
	SName      string `json:"s_name" form:"s_name"`
	Lon        string `json:"lon" form:"lon"`
	Lat        string `json:"lat" form:"lat"`
	Desc       string `json:"desc" form:"desc"`
	Level      int    `json:"level" form:"level"`
	AreaId     string `json:"area_id" form:"area_id"`
	ModelId    string `json:"model_id" form:"model_id"`
	StatusUsed int    `json:"status_used" form:"status_used" binding:"gte=-2,lte=2"` // 开通状态 -2 未知;1使用中;0停用;-1施工中; //"gte=0,lte=130"` //  0<=Age<=130
}

type ReqMgModifyStation struct {
	StationId  string `json:"station_id" form:"station_id" binding:"required"`
	SName      string `json:"s_name" form:"s_name" binding:"required"`
	Lon        string `json:"lon" form:"lon"`
	Lat        string `json:"lat" form:"lat"`
	Desc       string `json:"desc" form:"desc"`
	Level      int    `json:"level" form:"level"`
	AreaId     string `json:"area_id" form:"area_id" binding:"required"`
	ModelId    string `json:"model_id" form:"model_id" binding:"required"`
	StatusUsed int    `json:"status_used" form:"status_used" binding:"gte=-2,lte=2"` // 开通状态 -2 未知;1使用中;0停用;-1施工中; //"gte=0,lte=130"` //  0<=Age<=130
}

type ReqMgDelStation struct {
	StationId string `json:"station_id" form:"station_id" binding:"required"`
}
